package com.jumi.microservice.controller;

import com.jumi.microservice.common.base.BaseController;
import com.jumi.microservice.common.base.dto.BasePageResponse;
import com.jumi.microservice.common.base.dto.ResponseResult;
import com.jumi.microservice.common.core.aspect.WebLog;
import com.jumi.microservice.common.utils.ObjectUtils;
import com.jumi.microservice.common.utils.ResultGenerator;
import com.jumi.microservice.constant.ExceptionEnum;
import com.jumi.microservice.constant.NumberConstant;
import com.jumi.microservice.domain.*;
import com.jumi.microservice.service.CategoryService;
import com.jumi.microservice.service.LogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 商品分类管理controller组件
 *
 * @author Administrator
 */
@Api(tags = "商品分类相关接口")
@RestController
@RequestMapping("/commodity/category")
public class CategoryController extends BaseController {

    /**
     * 商品分类管理service组件接口
     */
    @Autowired
    private CategoryService categoryService;

    /**
     * 日志service组件
     */
    @Autowired
    private LogService logService;

    /**
     * 商品分类分页查询
     *
     * @param categoryQuery 商品分类查询条件
     * @return 商品分类VO集合
     */
    @ApiOperation(value = "商品分类分页查询", notes = "获取商品分类表分页数据")
    @GetMapping("/listPage")
    public BasePageResponse<List<CategoryVO>> listCategoryByPage(CategoryQuery categoryQuery) {
        List<CategoryVO> categoryVoList =
                convertCategoryDtoList2VoList(categoryService.listCategoryByPage(categoryQuery));
        return ResultGenerator.genSuccessResultPage(
                categoryVoList.stream()
                        .skip(categoryQuery.getPageSize() * (categoryQuery.getPageNum() - NumberConstant.NUMBER_ONE.intValue()))
                        .limit(categoryQuery.getPageSize()).collect(Collectors.toList())
                , Long.valueOf(categoryVoList.size()));
    }

    /**
     * 新增商品分类
     *
     * @param category 商品分类
     * @return 处理结果, 新增成功返回true
     */
    @ApiOperation(value = "新增商品分类", notes = "新增商品分类表数据")
    @PostMapping("/add")
    @WebLog(description = "新增商品分类")
    public ResponseResult<Boolean> saveCategory(@Validated @RequestBody CategoryVO category, HttpServletRequest request) {
        categoryService.saveCategory(convertCategoryVo2Dto(category), request);
        return ResultGenerator.genSuccessResult(Boolean.TRUE);
    }

    /**
     * 删除商品分类
     *
     * @param ids 账号ids,支持批量删除,商品分类ID以逗号分割
     * @return 处理结果, 删除成功返回true
     */
    @ApiOperation(value = "删除商品分类", notes = "删除指定ID商品分类表数据,支持批量删除,商品分类ID以逗号分割")
    @ApiImplicitParam(name = "ids", value = "ids", required = true)
    @DeleteMapping("/delete/{ids}")
    @WebLog(description = "删除商品分类")
    public ResponseResult<Boolean> deleteCategoryById(@PathVariable String ids, HttpServletRequest request) {
        categoryService.removeCategoryById(ids, request);
        return ResultGenerator.genSuccessResult(Boolean.TRUE);
    }

    /**
     * 更新商品分类
     *
     * @param category 商品分类
     * @return 处理结果, 新增成功返回true
     */
    @ApiOperation(value = "编辑商品分类", notes = "更新指定ID商品分类表数据")
    @PutMapping("/update")
    @WebLog(description = "编辑商品分类")
    public ResponseResult<Boolean> updateCategory(@RequestBody CategoryVO category, HttpServletRequest request) {
        categoryService.updateCategory(convertCategoryVo2Dto(category), request);
        return ResultGenerator.genSuccessResult(Boolean.TRUE);
    }

    /**
     * 根据商品分类id查询商品分类
     *
     * @param id 商品分类id
     * @return 商品分类VO
     */
    @ApiOperation(value = "查询商品分类详情", notes = "获取指定ID商品分类表数据")
    @ApiImplicitParam(name = "id", value = "ID")
    @GetMapping("/detail/{id}")
    public ResponseResult<CategoryVO> getCategoryById(@PathVariable("id") Long id) {
        CategoryDTO category = categoryService.getCategoryById(id);
        return ResultGenerator.genSuccessResult(convertCategoryDto2Vo(category));
    }

    /**
     * 商品分类状态启用/停用
     *
     * @param id     商品分类id
     * @param status 状态 (1启用，0.禁用)
     * @return 处理结果, 更新成功返回true
     */
    @ApiOperation(value = "商品分类状态启用/停用", notes = "商品分类状态的启用/停用")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", required = true),
            @ApiImplicitParam(name = "status", value = "状态", required = true)
    })
    @PutMapping("/enabled")
    @WebLog(description = "商品分类状态启用/停用")
    public ResponseResult<Boolean> enableStatus(@RequestParam("id") Long id, @RequestParam("status") Integer status,
                                                HttpServletRequest request) {
        categoryService.enableStatus(id, status, request);
        return ResultGenerator.genSuccessResult(Boolean.TRUE);
    }

    /**
     * 操作日志分页查询
     *
     * @param operateLogQuery 账号查询条件
     * @return 账号操作日志集合
     */
    @ApiOperation(value = "标签操作日志分页查询", notes = "标签操作日志分页数据")
    @GetMapping(value = "/log")
    public BasePageResponse<List<LogDetailVO>> lisOperateLogByPage(OperateLogQuery operateLogQuery) {
        BasePageResponse<List<LogDetailVO>> responseResult = logService.listOperateLogs(operateLogQuery);
        return responseResult;
    }

    /**
     * 返回商品分类列表(给商品管理列表页使用)
     *
     * @return 商品分类列集合
     */
    @ApiOperation(value = "商品管理-商品分类下拉项查询", notes = "商品管理-商品分类下拉项查询")
    @GetMapping(value = "/list")
    public ResponseResult<List<CategoryVO>> listCategory() {
        List<CategoryVO> categoryList = convertCategoryDtoList2VoList(categoryService.listCategory(null));
        return ResultGenerator.genSuccessResult(categoryList);
    }

    /**
     * 返回商品分类列表
     *
     * @param id 商品分类id
     * @return 商品分类列集合
     */
    @ApiOperation(value = "商品管理-添加商品-商品分类下拉列表", notes = "商品管理-添加商品-商品分类下拉列表")
    @ApiImplicitParam(name = "id", value = "商品分类的父ID")
    @GetMapping(value = "/listCategoryById")
    public ResponseResult<List<CategoryVO>> listCategoryById(@RequestParam("id") Long id) {
        List<CategoryVO> categoryList = convertCategoryDtoList2VoList(categoryService.listCategoryById(id));
        return ResultGenerator.genSuccessResult(categoryList);
    }

    /**
     * 返回商品分类列表(给商品管理列表页使用)
     *
     * @return 商品分类列集合
     */
    @ApiOperation(value = "商品分类列表", notes = "商品分类列表")
    @GetMapping(value = "/listSlip")
    public ResponseResult<List<CategorySlipVO>> listSlip() {
        List<CategoryDTO> list = categoryService.listCategory(null);
        return ResultGenerator.genSuccessResult(list.stream().map(c -> {
            CategorySlipVO vo = new CategorySlipVO();
            vo.setId(c.getId());
            vo.setCategoryName(c.getCategoryName());
            return vo;
        }).collect(Collectors.toList()));
    }

    /**
     * 将商品分类VO对象转换为DTO对象
     *
     * @param category 商品分类VO
     * @return CategoryDTO 商品分类DTO
     */
    private CategoryDTO convertCategoryVo2Dto(CategoryVO category) {
        CategoryDTO targetCategory = null;
        try {
            targetCategory = category.clone(CategoryDTO.class);
        } catch (Exception e) {
            ExceptionEnum.FAIL_CHANGE.newException();
        }
        return targetCategory;
    }

    /**
     * 将商品分类DTO对象转换为VO对象
     *
     * @param category 商品分类DTO
     * @return CategoryVO 商品分类vo
     */
    private CategoryVO convertCategoryDto2Vo(CategoryDTO category) {
        CategoryVO targetCategory = null;
        try {
            targetCategory = category.clone(CategoryVO.class);
        } catch (Exception e) {
            ExceptionEnum.FAIL_CHANGE.newException();
        }
        return targetCategory;
    }

    /**
     * 将商品分类DTO对象集合转换为VO对象集合
     *
     * @param categories 商品分类DTO集合
     * @return List<CategoryVO> 商品分类VO对象集合
     */
    private List<CategoryVO> convertCategoryDtoList2VoList(List<CategoryDTO> categories) {
        List<CategoryVO> categoryVoList = null;
        try {
            categoryVoList = ObjectUtils.convertList(categories, CategoryVO.class);
        } catch (Exception e) {
            ExceptionEnum.FAIL_CHANGE.newException();
        }
        return categoryVoList;
    }
}
